package com.datoucai.controller;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class ProducerController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/send")
    public SendResult sendMessage() {
        Message<String> message = MessageBuilder
                .withPayload("Hello RocketMQ 5.1.0!")
                .build();
        // 同步发送并获取结果
        SendResult sendResult = rocketMQTemplate.syncSend("YOUR_TOPIC", message);

        // 判断发送状态
        SendStatus status = sendResult.getSendStatus();
        if (status == SendStatus.SEND_OK) {
            System.out.println("消息发送成功，消息ID：" + sendResult.getMsgId());
        } else {
            System.out.println("消息可能未完全持久化，状态码：" + status);
            // 根据业务需求重试（例如FLUSH_DISK_TIMEOUT等状态）[1](@ref)
        }
        return sendResult;
    }
}